package cn.com.spring;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.util.concurrent.Future;

@Component
public class ThreadMethod {

    /**
     * 无返回值
     * @param msg
     */
    @Async
    public void taskDemo1(String msg){
        String threadName = Thread.currentThread().getName();
        for (int i = 0; i < 10; i++) {
            System.out.println(threadName + ", You can be Say: " + msg);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 有返回值，返回值类型必须是Future
     * @param msg
     * @return
     */
    @Async
    public Future<String> taskDemo2(String msg){
        String threadName = Thread.currentThread().getName();
        for (int i = 0; i < 10; i++) {
            System.out.println(threadName + ", You can be Say: " + msg);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String result = threadName + ", You can be Say: " + msg;
        return new AsyncResult(result);
    }
}
